<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Event\UserEvent;
class UserController extends AdminController {

    //用户列表
    public function index(){
        $a = A('User','Event');
        $a->index();
        $admin_name = session('name');
        $this->assign('admin_name',$admin_name);
        $this->display('User/index');
    }
    //用户删除
    public function del(){
        if(IS_GET){
            $id = I('get.id',0,'int');
            if(M('user')->delete($id)>0){
                $this->success('删除成功',U('User/index'));
            }else{
                $this->error('删除失败',U('User/index'));
            }
        }else{
            E('非法访问');
        }
    }
    public function add(){
        $this->display('User/add');
    }
    //执行用户插入
    public function adddo(){
//        dump($_POST);die;


        $a = A('User','Event');
        $a->adddo();
    }
    public function edit(){
        $id = I('get.id',0,'int');
        $ms = M('user');
        $res = $ms->where("id='$id'")->find();
        $this->assign('res',$res);
        $this->display('User/edit');
    }
    public function editdo(){
        $ms = M("user"); // 实例化User对象
        $data['name'] = $_POST['name'];
        $id = $_POST['id'];
        $data['pwd'] = md5($_POST['pwd']);
        $data['tel'] = $_POST['tel'];
        $data['sex'] = $_POST['sex'];
        $data['email'] = $_POST['email'];
        $res = $ms->where("id='$id'")->save($data);
//        dump($res);die;
        if($res){
            $this->success('修改成功',U('User/index'));
        }else{
            $this->error('修改失败',U('User/index'));
        }

    }


    //管理员列表
    public function admin_index(){
        $a = A('User','Event');
        $a->admin_index();
        $this->display('User/admin_index');
    }
    //管理员删除
    public function admin_del(){
        if(IS_GET){
            $id = I('get.id',0,'int');

            //查询,如果级别不够,则不能删除(待补充....)
            $type = $_SESSION['type'];
            if($type == 1){
                $this->error('管理级别不够,无法删除',U('User/admin_index'));
            }else{
                if(M('adminuser')->delete($id)>0){
                    $this->success('删除成功',U('User/admin_index'));
                }else{
                    $this->error('删除失败',U('User/admin_index'));
                }
            }


        }else{
            E('非法访问');
        }
    }
    public function admin_add(){
        $this->assign('tn',$_SESSION['type']==1?"disabled":'');
        $this->display('User/admin_add');
    }
    public function admin_adddo(){
        $a = A('User','Event');
        $a->admin_adddo();
    }

    public function admin_edit(){
        $id = I('get.id',0,'int');
        $ms = M('adminuser');
        $res = $ms->where("id='$id'")->find();
        $this->assign('res',$res);
        $this->assign('tn',$_SESSION['type']==1?"disabled":'');
        $this->display('User/admin_edit');
    }
    public function admin_editdo(){
        $ms = M("adminuser"); // 实例化User对象
        $data['name'] = $_POST['name'];
        $id = $_POST['id'];
        $data['pwd'] = md5($_POST['pwd']);
        $data['tel'] = $_POST['tel'];
        $data['type'] = $_POST['sex'];
        $data['email'] = $_POST['email'];
        $res = $ms->where("id='$id'")->save($data);
//        dump($res);die;
        if($res){
            $this->success('修改成功',U('User/admin_index'));
        }else{
            $this->error('修改失败',U('User/admin_index'));
        }

    }

}